<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/8/18
 * Time: 16:37
 */

namespace backend\modules\manager\controllers;
use backend\controllers\BaseController;
use common\models\Product;
use Yii;
use yii\data\ArrayDataProvider;
use yii\db\Query;
use backend\modules\manager\models\ProductForm;

class ProductController extends BaseController {

    /** 查询显示列 */
    public $selectParam = array(
        'product.id',
        'product.title',
        'product.banner_pic',
        'business.name',
        'product.created_at',
        'product.read_num',
        'product.share_num',
        'product.code_url',
        'product.status'
    );

    /**
     * 后台获取商家列表接口
     * @param page 页数(从0开始)
     * @param pageSize 每页显示条数
     * @param name 商家名称
     * @param title 作品名称
     * @param status 作品状态 -1已删除 0未审批 1已通过
     * @return array
     */
    public function actionList()
    {
        /** 当前页数 从0开始 */
        $page = $this->postData['page'] == null ? 0 : intval($this->postData['page']);
        /** 每页显示条数 */
        $pageSize = $this->postData['pageSize'] == null ? 10 : intval($this->postData['pageSize']);

        /** 组装where查询条件 */
        $where = array();
        /** 商家名称 */
        if(isset($this->postData['name'])){
            $where['business.name'] = trim($this->postData['name']);
        }
        /** 作品名称 */
        if(isset($this->postData['title'])){
            $where['product.title'] = trim($this->postData['title']);
        }
        /** 作品状态 */
        if(isset($this->postData['status'])){
            $where['product.status'] = intval($this->postData['status']);
        }

        /** 定义查询对象 */
        $query = new Query();
        $provider = new ArrayDataProvider([
            'allModels' => $query->select($this->selectParam)->from('product')
                ->innerJoin('business', 'product.businessId = business.id')
                ->where($where)->all(),
            'sort' => [
                'attributes' => ['product.id'],
            ],
            'pagination' => [
                'page' => $page,
                'pageSize' => $pageSize,
            ],
        ]);

        $datas = array();
        /** 获取数据 */
        $models = $provider->getModels();
        if($models){
            foreach($models as $key => $value){
                $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
                $value['updated_at'] = date('Y-m-d H:i:s', $value['updated_at']);
                $datas[] = $value;
            }
        }
        /** 数据总条数 */
        $totalCount = $provider->getTotalCount();

        /** 返回数据 */
        $returnData = array();
        $returnData['list'] = $datas;
        $returnData['totalCount'] = $totalCount;
        return $this->responseFormat($returnData);
    }

    /**
     * 查询作品详情
     * @param pid 作品Id
     * @return array
     */
    public function actionView()
    {
        /** 获取商家ID */
        $id = $this->postData['pid'] == null ? 0 : intval($this->postData['pid']);
        if(!$id){
            return $this->responseError('作品Id不能为空.');
        }

        /** 获取商家信息 */
        $product = Product::findIdentity(array('id' => $id));
        if(!$product){
            return $this->responseError('没有匹配到对应作品信息.');
        }
        /** 返回数据 */
        $returnData = array();
        $returnData['detail'] = $product;
        return $this->responseFormat($returnData);
    }

    /**
     * 编辑作品信息
     * @param pid 作品Id
     * @param title 作品名称
     * @param banner_pic 封面图
     * @param description 作品描述
     * @param is_brand 是否显示品牌 0否 1是
     * @param is_home 是否显示商家主页 0否 1是
     * @param music 背景音乐路径
     * @param change_status 页面切换 1上下切换 2折叠切换 3立体切换
     * @param pics 上传图片
     */
    public function actionEdit()
    {
        /** 校验表单是否为空 */
        if(!$this->postData){
            return $this->responseError("参数信息为空.");
        }
        /** 获取商家ID */
        $id = $this->postData['pid'] == null ? 0 : intval($this->postData['pid']);
        if(!$id){
            return $this->responseError('作品Id不能为空.');
        }

        /** 定义返回数据 */
        $resultData = array();
        /** 接收数据 */
        $productForm = new ProductForm();
        $productForm->id = $this->postData['pid'];
        $productForm->title = $this->postData['title'];
        $productForm->description = $this->postData['description'];
        $productForm->is_brand = $this->postData['is_brand'];
        $productForm->is_home = $this->postData['is_home'];
        $productForm->music = $this->postData['music'];
        $productForm->change_status = $this->postData['change_status'];
        $productForm->pics = $this->postData['pics'];
        $productForm->banner_pic = $this->postData['banner_pic'];

        /** 添加 */
        $bool = $productForm->add();
        if($bool){
            /** 成功返回 */
            $resultData['id'] = '';
            return $this->responseFormat($resultData);
        }

        /** 失败返回 */
        if ($productForm->errors) {
            foreach ($productForm->errors as $key => $info) {
                $resultData[$key] = $info;
            }
        }
        return $this->responseError($resultData);
    }

    /**
     * 作品审批接口
     * @param pid 作品Id
     * @param status 作品状态 -1已删除 0未审批 1已通过'
     */
    public function actionAudit()
    {
        /** 校验表单是否为空 */
        if(!$this->postData){
            return $this->responseError("参数信息为空.");
        }
        /** 校验id */
        if(empty($this->postData['pid']) || empty($this->postData['status'])){
            return $this->responseError('Id is null');
        }

        /** 获取商家信息 */
        $product = Product::findIdentity($this->postData['pid']);
        if($product){
            $product['status'] = intval($this->postData['status']);
            /** 更新状态 */
            $bool = $product->save(false);
            if($bool){
                /** 成功返回 */
                $resultData = array(
                    'id' => $product['id']
                );
                return $this->responseFormat($resultData);
            }else{
                /** 失败 */
                return $this->responseFail('error');
            }
        }
        return $this->responseError('product info not exist');
    }
}